package com.goodving.guli.bt.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import com.goodving.guli.R;
import com.goodving.guli.view.C3657a;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.com.smartdevices.bracelet.chart.util.C0804t;

public class WeightUserAvatar extends ImageView {
    private String f10843a;
    private String f10844b;
    private boolean f10845c;
    private Bitmap f10846d;
    private float f10847e;
    private int f10848f;
    private int f10849g;

    public WeightUserAvatar(Context context) {
        this(context, null, 0);
    }

    public WeightUserAvatar(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    class C1904Z implements OnLayoutChangeListener {
        final /* synthetic */ WeightUserAvatar f10863a;

        C1904Z(WeightUserAvatar weightUserAvatar) {
            this.f10863a = weightUserAvatar;
        }

        public void onLayoutChange(View view, int i, int i2, int i3, int i4, int i5, int i6, int i7, int i8) {
            if (this.f10863a.f10845c) {
                this.f10863a.m15372b();
            }
        }
    }

    public WeightUserAvatar(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.f10849g = 436207616;
        addOnLayoutChangeListener(new C1904Z(this));
    }

    private void m15370a() {
        this.f10847e = C0804t.m8825a(getContext(), 23.0f);
        this.f10848f = -419430401;
        this.f10846d = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(this.f10846d);
        Paint paint = new Paint(1);
        paint.setTextAlign(Align.CENTER);
        paint.setLinearText(true);
        paint.setTextSize(this.f10847e);
        paint.getTextBounds(this.f10844b, 0, 1, new Rect());
        float width = (float) (canvas.getWidth() / 2);
        float height = (float) (canvas.getHeight() / 2);
        paint.setColor(this.f10849g);
        canvas.drawCircle(width, height, width, paint);
        paint.setColor(0xe6ffffff);
        canvas.drawText(this.f10844b, width, height - ((paint.descent() + paint.ascent()) / 2.0f), paint);
    }

    private void m15372b() {
        if (this.f10846d == null && !TextUtils.isEmpty(this.f10844b)) {
            m15370a();
        }
        super.setBackground(new BitmapDrawable(getResources(), this.f10846d));
    }

    public void m15374a(int i) {
        this.f10849g = i;
    }

    public void m15375a(String str) {
        String str2 = null;
        if (str != null && !str.equals(this.f10843a)) {
            this.f10843a = str;
            this.f10846d = null;
            this.f10844b = C3657a.f18328f;
            try {
                this.f10844b = this.f10843a.substring(0, 1);
                this.f10844b = this.f10844b.toUpperCase();
                Matcher matcher = Pattern.compile("[\u4e00-\u9fa5]+").matcher(this.f10843a);
                while (matcher.find()) {
                    str2 = matcher.group();
                }
                if (!TextUtils.isEmpty(str2)) {
                    this.f10844b = str2.substring(str2.length() - 1, str2.length());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void setBackground(Drawable drawable) {
        this.f10845c = false;
        super.setBackground(drawable);
    }

    public void setBackgroundResource(int i) {
        if (i == R.drawable.default_userinfo) {
            this.f10845c = true;
            if (getWidth() > 0) {
                m15372b();
                return;
            }
            return;
        }
        this.f10845c = false;
        super.setBackgroundResource(i);
    }
}
